﻿using Reference.Apps.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Reference.Apps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.SizeChanged += MainPage_SizeChanged;
        }

        void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width < 700)
            {
                VisualStateManager.GoToState(this, "Snapped", true);
            }
            else
            {
                VisualStateManager.GoToState(this, "Default", true);
            }
        }

        private void SnippetsListItem_Click(object sender, ItemClickEventArgs e)
        {
            (this.DataContext as MainPageViewModel).CodeViewSnippet = e.ClickedItem as Reference.Apps.Models.Entities.CodeSnippet;
            this.Frame.Navigate(typeof(CodeView));
        }

        private void HeaderButton_Click(object sender, RoutedEventArgs e)
        {
            (this.DataContext as MainPageViewModel).SelectedCodeGroup = ((sender as Button).DataContext) as Reference.Apps.Models.Entities.CodeGroup;
            this.Frame.Navigate(typeof(GroupView));
        }

        private void SearchBox_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args)
        {
            this.Frame.Navigate(typeof(SearchView), args.QueryText);
        }
    }
}
